package com.eat.controller.admin;

import com.eat.dto.SetmealDTO;
import com.eat.dto.SetmealPageQueryDTO;
import com.eat.result.PageResult;
import com.eat.result.Result;
import com.eat.service.SetmealService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){

        setmealService.saveWithSetmealDish(setmealDTO);

        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){

       PageResult pageResult = setmealService.page(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 起售,停售
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result putStatus(@PathVariable Integer status ,Long id){

        setmealService.putStatus(status,id);

        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result putSetmeal(@RequestBody SetmealDTO setmealDTO){

        setmealService.putSetmeal(setmealDTO);

        return Result.success();
    }

    /**
     * 根据id 查套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
//    @Cacheable(cacheNames = "")
    public Result<SetmealDTO> getSetmealById(@PathVariable Long id){

        SetmealDTO setmealDTO =  setmealService.getSetmealById(id);

        return Result.success(setmealDTO);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result deleteSetmealByIds(@RequestParam List<Long> ids){
        log.info("批量删除套餐");
        setmealService.deleteByIds(ids);

       return Result.success();
    }
}
